package com.adlotto2.lcdns.activity.ad;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.adlotto2.lcdns.R;
import com.adlotto2.lcdns.activity.ad.adapter.LottoAdJobListAdapter;
import com.adlotto2.lcdns.common.BaseActivity;

public class LottoAdJobChoiceActivity extends BaseActivity {
	public static final int MODE_REQUEST = 10000;
	public static final int MODE_OTHER = 20000;
	
	private int mCurrentMode = 0;
	
	private Context mContext;
	private ListView mJobList;
	private ProgressBar mProgressBar;
	private LottoAdJobListAdapter mAdapter;
	
	private ArrayList<String> mJobCodeList, mJobNameList;
	
	private Button mChoiceCompleteButton;
	@Override
	protected int onGetContentViewResource() {
		return R.layout.lottoad_job_coice_activity;
	}
	
	@Override
	protected void onInit() {
		mContext = this;
		String extra = getIntent().getStringExtra("requestMode");
		if(extra != null) {
			if(extra.equals(String.valueOf(MODE_REQUEST))) {
				mCurrentMode = MODE_REQUEST;
			} else {
				mCurrentMode = MODE_OTHER;
			}
		} else {
			mCurrentMode = MODE_OTHER;
		}
		
		initViews();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		onRequestData();
	}
	
	private void initViews() {
		mJobList = (ListView) findViewById(R.id.lv_job_list);
		mJobList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		mJobList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(mCurrentMode == MODE_OTHER) {
					String selectedJob = mJobNameList.get(position);//jobNameText.getText().toString();
					String selectedJobCode = mJobCodeList.get(position);
					Intent intent = new Intent();
					intent.putExtra("job", selectedJob);
					intent.putExtra("jobCode", selectedJobCode);
					
					setResult(RESULT_OK,intent);
					finish();
				}
			}
		});
		
		mAdapter = new LottoAdJobListAdapter(mContext, mCurrentMode);
		mJobList.setAdapter(mAdapter);
		
		mProgressBar = (ProgressBar) findViewById(R.id.pb_job_list_progress);
		mChoiceCompleteButton = (Button) findViewById(R.id.btn_choice_complete);
		
		if(mCurrentMode == MODE_REQUEST) {
			mChoiceCompleteButton.setVisibility(View.VISIBLE);
			mChoiceCompleteButton.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(mAdapter.getCheckedItemCount() == 0) {
						AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
						alert.setTitle(R.string.popup_alert).
						setMessage(R.string.alert_no_selected).
						setPositiveButton(R.string.commons_ok, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
								goBack();
							}
						}).
						setNegativeButton(R.string.commons_cancel, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						}).show();
						
					
					} else {
						Intent intent = new Intent();
						intent.putExtra("selectedJobsCode", mAdapter.getCheckedItemsCode());
						intent.putExtra("selectedJobs", mAdapter.getCheckedItemsName());
						intent.putExtra("selectedJobsCount", String.valueOf(mAdapter.getCheckedItemCount()));
						setResult(RESULT_OK,intent);
						finish();
					}
				}
			});
		} else {
			mChoiceCompleteButton.setVisibility(View.GONE);
		}
	}

	@Override
	public void onAsyncPreRequest() {
		// TODO Auto-generated method stub
		
	}
	
	public void goBack() {
		super.onBackPressed();
	}

	@Override
	public void onAsyncRequestCompleted(Element xmlObject) {
		if(parseJobList(xmlObject) != -1) {
			mAdapter.setJobNameListArray(mJobNameList);
			mAdapter.setJobCodeListArray(mJobCodeList);
			mAdapter.notifyDataSetChanged();
			mProgressBar.setVisibility(View.GONE);
		}
	}

	@Override
	public void onAsyncCancelRequest() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String onGetAsyncRequestUrl() {
		return "http://ad.lcdns.kr/server/BusinessList.asp";
	}

	@Override
	public void onAsyncRequestError(int paramInt) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAsyncDataError(Element paramXMLObject, String paramString) {
		// TODO Auto-generated method stub
		
	}
	
	private int parseJobList(Element xmlObject) {
		mJobCodeList = new ArrayList<String>();
		mJobNameList = new ArrayList<String>();
		
		try {
			
			NodeList jobList = xmlObject.getElementsByTagName("business");
			
			NodeList jobCodeList = xmlObject.getElementsByTagName("code");
			NodeList jobNameList = xmlObject.getElementsByTagName("name");
			
			if(jobList != null) {
				int count = jobList.getLength();
				
				
				
				
				
				for(int i = 0; i < count; i++) {
					String jobCode = "";
					String jobName = "";
					
					Element elementData = (Element) jobList.item(i);
					if (elementData == null)
						continue;
					
					if(jobCodeList.item(i).getFirstChild() != null)
						jobCode = jobCodeList.item(i).getFirstChild().getNodeValue();
					if(jobNameList.item(i).getFirstChild() != null)
						jobName = jobNameList.item(i).getFirstChild().getNodeValue();
					
					mJobCodeList.add(jobCode);
					mJobNameList.add(jobName);
					
				}
				return mJobNameList.size();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	

	

}
